from django.contrib import admin
from reversion.admin import VersionAdmin
from gxjg.models import *
from gxjg.resources import *
from import_export.admin import ImportExportModelAdmin, ExportActionModelAdmin


@admin.register(Gxjg0301)
class Gxjg0301Admin(VersionAdmin, ExportActionModelAdmin):
    list_display = ['gh', 'teacher', 'zwmc', 'zwlbm', "zwpx", 'zwjbm', 'rzbm', 'rzks', 'dqrzzt', 'priority', 'lock', 'source', 'remarks', 'update_time']
    list_filter = ['zwmc', 'zwlbm', 'zwjbm', 'dqrzzt', 'rzbm', 'rzks', 'source', 'lock', 'create_time', 'update_time']
    search_fields = ['teacher__gh', 'teacher__xm', 'zwmc']
    ordering = ['-update_time']

    fieldsets = (
        ('职务信息', {"fields": ["teacher", ("rzbm", "rzks"), ("zwmc", "zwmcm"), "zwpx", ("zwlbm", "zwflm", "zwjbm"), "zghcsdgz"]}),
        ("任免信息", {"fields": ["dqrzzt", ("rzrq", "rzfsm"), ("rzyy", "rzwh"), "rzpzdw", "zwbdlbm", ("mzrq", "mzfsm"), ("mzyym", "mzwh"), "mzpzdw"]}),
        ("其他信息", {"fields": ['source', 'priority', 'lock', 'remarks', 'create_time', 'update_time']}),
    )
    readonly_fields = ['create_time', 'update_time']
    raw_id_fields = ('teacher', )
    save_on_top = True
    resource_class = Gxjg0301Resource

    def save_model(self, request, obj, form, change):
        if not obj.source:
            obj.source = '系统手工添加'
        super(Gxjg0301Admin, self).save_model(request, obj, form, change)

    def gh(self, obj):
        return obj.teacher.gh

    gh.short_description = '工号'
